package com.kiss.fw.context;

import java.io.Serializable;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.kiss.fw.utils.Constants;

public class ActionContext extends AbstractContext implements Serializable {
	private HttpServletRequest request;
	private HttpSession session;
	private HttpServletResponse response;
	private String action;
	
	public ActionContext(final HttpServletRequest _request, final HttpServletResponse _response) {
		request = _request;
		response = _response;
		session = _request.getSession();
		action = _request.getParameter(Constants.Mappings.WEB_ACTION);
		
		// if null, default to read
		if(action == null) {
			action = Constants.Mappings.READ_ACTION;
		}
	}
	
	public HttpServletRequest getRequest() {
		return request;
	}
	
	public HttpServletResponse getResponse() {
		return response;
	}
	
	public HttpSession getSession() {
		return session;
	}
	
	public void setRequestAttribute(final String _key, final Object _value) {
		request.setAttribute(_key, _value);
	}
	
	public void setSessionAttribute(final String _key, final Object _value) {
		session.setAttribute(_key, _value);
	}
	
	public boolean isCreateAction() {
		return isAction(Constants.Mappings.CREATE_ACTION);
	}
	
	public boolean isReadAction() {
		return isAction(Constants.Mappings.READ_ACTION);
	}
	
	public boolean isUpdateAction() {
		return isAction(Constants.Mappings.UPDATE_ACTION);
	}
	
	public boolean isDeleteAction() {
		return isAction(Constants.Mappings.DELETE_ACTION);
	}
	
	public boolean isSearchAction() {
		return isAction(Constants.Mappings.SEARCH_ACTION);
	}
	
	public boolean isBrowseAction() {
		return isAction(Constants.Mappings.BROWSE_ACTION);
	}
	
	public boolean isAction(final String _action) {
		return _action.equals(action);
	}
}